/**
 * 
 */
package org.compiere.process;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.logging.Level;

import org.adempiere.exceptions.AdempiereException;
import org.compiere.model.MBPartner;
import org.compiere.model.Mhrmloan;
import org.compiere.model.Mhrmloanline;
import org.compiere.model.Mhrmreserve;
import org.compiere.model.Mhrmreservesectional;
import org.compiere.model.Mhrmreviewline;
import org.compiere.model.X_hrm_loan;
import org.compiere.model.X_hrm_loan_line;
import org.compiere.model.X_hrm_loanline;
import org.compiere.model.X_hrm_reviewline;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.eevolution.model.X_HR_Employee;
import extend.org.compiere.utils.Util;
/**
import extend.org.compiere.hrm.TaxCompute;
import extend.org.compiere.hrm.TaxVariable;
import extend.org.compiere.hrm.TypeTax;
*/

/**
 * @author A.Yerzhanov
 * 07.12.2012
 */
public class GenerateLoanPayment extends SvrProcess {
	
	private int C_BPartner_ID = 0;
	private int HR_Block_ID  = 0;
	private int HR_Department_ID  = 0;
	double SUM = .0;
	Timestamp dateFrom = null;
	
	/** Set calendar */
	Calendar cMonth = Calendar.getInstance();
		
	/**	Employee List	*/
	private MBPartner[] m_lines;
	
	/** Tax calculation*/
	// private TaxVariable taxcomp = null;
	
	private Mhrmloan loan = null;
	private Mhrmloanline loanline = null;
	
	/** */
	String closeTax = "N";
	
	@Override
	protected void prepare() {
		
		ProcessInfoParameter[] para = getParameter();
		for (int i = 0; i < para.length; i++)
		{
				String name = para[i].getParameterName();
				if (name.equals("CloseTax"))
					closeTax = para[i].getParameter().toString();
				else
					log.log(Level.INFO, "Unknown Parameter: " + name);			
		}
		
		if(getRecord_ID() > 0)
			loan = new Mhrmloan(getCtx(), getRecord_ID(), null);

	}
	
	@Override
	protected String doIt() throws Exception {
			
		if(loan.getReversal_ID() != 0)
			return null;
		else if(loan.get_ID() == 0)
			return null;
		
		String deleteOldLines = "DELETE FROM " + X_hrm_loan_line.Table_Name + " WHERE " + X_hrm_loan.COLUMNNAME_hrm_loan_ID + "=" + loan.get_ID();
		DB.executeUpdate(deleteOldLines, null);
		
		C_BPartner_ID = loan.getC_BPartner_ID();
		HR_Block_ID = loan.getHR_Block_ID();
		HR_Department_ID = loan.getHR_Department_ID();
		dateFrom = loan.getStartDate();
		
		MBPartner bp = new MBPartner(getCtx(), C_BPartner_ID, null);
		X_HR_Employee he = null;
		m_lines = bp.getPartnerEmployeeDate(C_BPartner_ID, HR_Block_ID, HR_Department_ID, dateFrom, true);
		
		//
		for(MBPartner b: m_lines){
			// Title
			  he = X_HR_Employee.getHR_Employee(b.getC_BPartner_ID(),true);
			   if (he.getC_BPartner_ID() == 0)
					throw new AdempiereException ("Info about employee was empty");
			   
			   int Month = Util.getMonth(loan.getStartDate(), loan.getEndDate());
			   int M = Month + 1;
			   BigDecimal DIV = loan.gethrm_total().divide(new BigDecimal(M), BigDecimal.ROUND_UP);	
			   for (int i = 0; i <= Month; i++){				   
				   cMonth.setTime(dateFrom);
				   loanline = Mhrmloanline.getLoanList(b.getC_BPartner_ID(), dateFrom);
				   if (loanline == null)
					   loanline = new Mhrmloanline(getCtx(), 0, null);
				   loanline.sethrm_loan_ID(loan.gethrm_loan_ID());
				   loanline.setC_BPartner_ID(b.getC_BPartner_ID());
				   loanline.setHR_Department_ID(he.getHR_Department_ID());
				   loanline.setHR_Job_ID(he.getHR_Job_ID());
				   loanline.setDateFrom(dateFrom);				   			   			   
				   				   
				   /** Generate employee tax */
				   /* SUM = DIV.doubleValue();				   
				   taxcomp =  TaxCompute.GetTax(b.getC_BPartner_ID(), SUM, dateFrom, TypeTax.LOAN, closeTax);
				   SUM = SUM + taxcomp.getIPN() + taxcomp.getOPV();
				   loanline.sethrm_IPN(new BigDecimal(taxcomp.getIPN()));
				   loanline.sethrm_OPV(new BigDecimal(taxcomp.getOPV()));
				   loanline.sethrm_CNalog(new BigDecimal(taxcomp.getCNALOG() - taxcomp.getCOTCH()));
				   loanline.sethrm_COtch(new BigDecimal(taxcomp.getCOTCH()));
				   */
				   loanline.sethrm_total(DIV);
				   loanline.sethrm_amount(new BigDecimal(SUM));
				   loanline.saveEx();				   
				   
//				   Mhrmreservesectional[] line = Mhrmreservesectional.getMhrmreservesectional(); 
//					for(Mhrmreservesectional l: line){
//						X_hrm_loanline loanLine = Mhrmloanline.getLoanLineAcct(loanline.gethrm_loan_line_ID(), l.gethrm_reservesectional_ID());
//						if(loanLine == null)
//							loanLine = new X_hrm_loanline(Env.getCtx(),0,null);
//						
//						if(l.gethrm_reservesectional_ID() == Mhrmreserve.RESERVE_SECTIONAL_CHARGE){
//							loanLine.sethrm_amount(loanline.gethrm_total());
//							loanLine.sethrm_reservesectional_ID(Mhrmreserve.RESERVE_SECTIONAL_CHARGE);
//						}
//						else if(l.gethrm_reservesectional_ID() == Mhrmreserve.RESERVE_SECTIONAL_CNALOG){
//							loanLine.sethrm_amount(loanline.gethrm_CNalog());
//							loanLine.sethrm_reservesectional_ID(Mhrmreserve.RESERVE_SECTIONAL_CNALOG);
//						}
//						else if(l.gethrm_reservesectional_ID() == Mhrmreserve.RESERVE_SECTIONAL_COTCH){
//							loanLine.sethrm_amount(loanline.gethrm_COtch());
//							loanLine.sethrm_reservesectional_ID(Mhrmreserve.RESERVE_SECTIONAL_COTCH);
//						}
//						loanLine.sethrm_reserve_ID(Mhrmreserve.RESERVE_VACATION);
//						loanLine.sethrm_loan_ID(loan.gethrm_loan_ID());
//						loanLine.sethrm_loan_line_ID(loanline.gethrm_loan_line_ID());
//						loanLine.saveEx();
//					}
					
				   cMonth.add(Calendar.MONTH, 1);
				   dateFrom = new Timestamp(cMonth.getTimeInMillis());
				   
			   }		   
			   
			 /**  if (closeTax.equals("Y")){
				   loanline.setProcessed(true);
				   loanline.saveEx();
				   loan.setProcessed(true);
				   loan.saveEx();
			   } */			
		 
		}
		return Msg.translate(Env.getCtx(), "Success");
	}
}
